package reduce.fz.com.reduce.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.AttentionUser;
import reduce.fz.com.reduce.bean.Card;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.custom.CardOperBtn;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;
import reduce.fz.com.reduce.custom.TabIndicator;

/**
 * Created by qingshengzheng on 15/7/22.
 */
public class CardListAdapter extends HeaderFooterStatusRecyclerViewAdapter<CardListAdapter.CardViewHolder> {

    private List<Card> cardList;

    @Override
    protected int getHeaderItemCount() {
        return 0;
    }

    @Override
    protected int getContentItemCount() {
        return 0;
    }

    @Override
    protected CardViewHolder onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        return null;
    }

    @Override
    protected CardViewHolder onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_card_item, null);
        return new CardViewHolder(view, operListener);
    }

    @Override
    protected void onBindHeaderItemViewHolder(CardViewHolder headerViewHolder, int position) {
        ;

    }

    @Override
    protected void onBindContentItemViewHolder(CardViewHolder contentViewHolder, int position) {
        contentViewHolder.renderView(cardList.get(position));
    }


    public void addCardList(List<Card> list) {
        int start = cardList.size();
        cardList.addAll(list);
        notifyItemRangeChanged(start, list.size());
    }

    public void clearData() {
        cardList.clear();

        notifyDataSetChanged();
    }


    private CardOperListener operListener = new CardOperListener() {
        @Override
        public void zan(Card card) {

        }

        @Override
        public void pinglun(Card card) {

        }

        @Override
        public void fenxiang(Card card) {

        }

        @Override
        public void guanzhu(Card card) {

        }

        @Override
        public void cardDetai(Card card) {

        }

        @Override
        public void userDetail(User user) {

        }

        @Override
        public void attentionUserDetail(AttentionUser user) {

        }

        @Override
        public void attentionUserList(Card card) {

        }
    };

    @Override
    public CardViewHolder createFooterStatusViewHolder(View footerView) {
        return null;
    }

    public static interface CardOperListener {

        public void zan(Card card);

        public void pinglun(Card card);

        public void fenxiang(Card card);

        public void guanzhu(Card card);

        public void cardDetai(Card card);

        public void userDetail(User user);

        public void attentionUserDetail(AttentionUser user);

        public void attentionUserList(Card card);


    }

    public static class CardViewHolder extends RecyclerView.ViewHolder implements TabIndicator.OnTabSelectedListener, OnClickListener {
        private ImageView mIvUserFace;
        private TextView mTvUserNickName;
        private TextView mTvPubTime;
        private TextView mTvPubContent;
        private TextView mCardReplys;
        private TabIndicator mAttentionUsers;
        private CardOperBtn mBtnZan;
        private CardOperBtn mBtnPinglun;
        private CardOperBtn mBtnFenxiang;
        private CardOperBtn mBtnGuanzhu;
        private CardOperListener operListener;
        private Card card;

        public CardViewHolder(View itemView, CardOperListener listener) {
            super(itemView);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_user_face:
                    operListener.userDetail(card.getUser());
                    break;
                case R.id.tv_user_nickname:
                    operListener.userDetail(card.getUser());
                    break;
                case R.id.btn_zan:
                    operListener.zan(card);
                    break;
                case R.id.btn_fenxiang:
                    operListener.fenxiang(card);
                    break;
                case R.id.btn_guanzhu:
                    operListener.guanzhu(card);
                    break;
                case R.id.btn_pinglun:
                    operListener.pinglun(card);
                    break;
                default:
                    break;
            }

        }

        public void initWidget() {
            mIvUserFace = (ImageView) itemView.findViewById(R.id.iv_user_face);
            mTvUserNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mTvPubTime = (TextView) itemView.findViewById(R.id.card_cotnent);
            mCardReplys = null;//(TextView) itemView.findViewById(R.id.card_replys);
            mAttentionUsers = (TabIndicator) itemView.findViewById(R.id.attention_users);
            mBtnZan = (CardOperBtn) itemView.findViewById(R.id.btn_zan);
            mBtnPinglun = (CardOperBtn) itemView.findViewById(R.id.btn_pinglun);
            mBtnFenxiang = (CardOperBtn) itemView.findViewById(R.id.btn_fenxiang);
            mBtnGuanzhu = (CardOperBtn) itemView.findViewById(R.id.btn_guanzhu);
        }

        public void initListener() {
            mIvUserFace.setOnClickListener(this);
            mTvUserNickName.setOnClickListener(this);
            mBtnZan.setOnClickListener(this);
            mBtnFenxiang.setOnClickListener(this);
            mBtnGuanzhu.setOnClickListener(this);
        }


        public void renderView(Card card) {
            this.card = card;
            //Picasso.with(itemView.getContext()).load(card.getUser().getHeadurl()).into(mIvUserFace);
            //mTvUserNickName.setText(card.getUser().getName());
            mTvPubTime.setText(card.getPubdate().toString());
            mCardReplys.setText(card.getReplys().toString());
            mAttentionUsers.setOnTabSelectedListener(this);

        }

        @Override
        public void onTabReselected(int position) {

        }

        @Override
        public void onTabSelected(int position) {

        }


    }
}
